﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Statistics
{
    /// <summary>
    /// This class provides the creation of an exponential smoothing timeseries.
    /// </summary>
    [Serializable]
    public class ExponentialSmoothingCreator
    {
        /// <summary>
        /// This field holds the timeseries from which the exponential smoothing should be computed.
        /// </summary>
        private TimeSeries series;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExponentialSmoothingCreator"/> class.
        /// </summary>
        /// <param name="series">The timeseries from which the exponential smoothing should be computed.</param>
        public ExponentialSmoothingCreator(TimeSeries series)
        {
            if (series == (TimeSeries) null)
            {
                throw new ArgumentNullException("series");
            }

            if (series.Data == (GeneralVector) null)
            {
                throw new ArgumentNullException("series");
            }

            this.series = series;
        }

        /// <summary>
        /// Gets or sets the timeseries from which the exponential smoothing should be computed.
        /// </summary>
        /// <value>The timeseries from which the exponential smoothing should be computed.</value>
        public TimeSeries Series
        {
            get { return this.series; }
            set { this.series = value; }
        }

        /// <summary>
        /// Creates the exponential smoothing timeseries. This method uses a default alpha value of 0.3.
        /// </summary>
        /// <returns>The created exponential smoothing timeseries.</returns>
        public TimeSeries CreateExponentialSmoothingSeries()
        {
            return this.CreateExponentialSmoothingSeries(0.3);
        }

        /// <summary>
        /// Creates the exponential smoothing timeseries.
        /// </summary>
        /// <param name="alpha">The specified alpha value for the smoothing process. Domaint for the alpha 
        /// parameter is [0...1]</param>
        /// <returns>
        /// The created exponential smoothing timeseries.
        /// </returns>
        public TimeSeries CreateExponentialSmoothingSeries(double alpha)
        {
            if ((alpha < 0) || (alpha > 1))
            {
                throw new ArgumentException("alpha");
            }

            GeneralVector result = new GeneralVector(this.series.Data.Count);

            result[0] = this.series.Data[0];

            for (int i = 1; i < this.series.Data.Count; i++)
            {
                result[i] = alpha*this.series.Data[i] + (1 - alpha)*result[i - 1];
            }

            return new TimeSeries(result);
        }
    }
}